package StringExercise;

import java.util.Random;
import java.util.Scanner;

/**
 * ClassName:StringExercise001
 * Package:StringExercise
 * Description:
 *
 * @Author:ZhangXin
 * @Create2023/1/17-17:24
 * @Version:v1.0
 */
public class StringExercise001 {
    public static void main(String[] args) {
        //键盘输入任意字符串,打乱里面的内容
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串");
        String str = sc.next();

        //打乱里面的内容
        //修改字符串里面的内容
        //1.subString
        //2.变成字符数组
        char [] arr = str.toCharArray();
        //打乱数组里面的内容
        //0索引和随机索引进行位置交换
        System.out.println("打乱后的字符串是：");
        String result = getNewArr(str);
        System.out.println(result);

        }

    //打乱数组方法
    public static String getNewArr(String str) {
        char[] arr = str.toCharArray();
//        char first = arr[0];
        for (int i = 0; i < arr.length; i++) {
            Random r = new Random();
            int index = r.nextInt(0,arr.length - 1);
            char temp = arr[i];
            arr[i] = arr[index];
            arr[index]=temp;
        }
        String result = new String(arr);
        return result;
    }
}
